<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="72px">
      <el-form-item label="标题" prop="title"><el-input v-model="queryParams.title" placeholder="请输入标题" clearable @keyup.enter.native="search" /></el-form-item>
      <el-form-item label="审核状态" prop="auditFlag">
        <el-select v-model="queryParams.auditFlag" placeholder="请选择审核状态" clearable>
          <el-option v-for="dict in auditFlagOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="发布标志" prop="releaseFlag">
        <el-select v-model="queryParams.releaseFlag" placeholder="请选择发布标志" clearable>
          <el-option v-for="dict in releaseFlagOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5"><el-button type="primary" icon="el-icon-plus" @click="handleAdd" v-hasPermi="['management:content:add']">新增</el-button></el-col>
      <el-col :span="1.5"><el-button type="success" icon="el-icon-edit" :disabled="single" @click="handleUpdate" v-hasPermi="['management:content:edit']">修改</el-button></el-col>
      <el-col :span="1.5"><el-button type="info" icon="el-icon-s-platform" :disabled="single" @click="handleView">查看</el-button></el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['management:content:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" border :data="contentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="封面" align="center" prop="showPictures" width="140" v-if="serviceInfo == 2 || serviceInfo == 3">
        <template slot-scope="scope">
          <el-image class="lsw_square" :fit="'contain'" :src="baseUrl + scope.row.showPictures" :preview-src-list="[baseUrl + scope.row.showPictures]"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="标题" align="left" prop="title" />
      <el-table-column label="作者" align="center" prop="author" width="190" />
      <el-table-column label="审核状态" align="center" prop="auditFlag" width="110">
        <template slot-scope="scope">
          <el-tag type="danger" effect="dark" v-if="scope.row.auditFlag == 0">未审核</el-tag>
          <el-tag effect="dark" v-if="scope.row.auditFlag == 1">初审通过</el-tag>
          <el-tag type="success" effect="dark" v-if="scope.row.auditFlag == 2">终审通过</el-tag>
          <el-tag type="danger" effect="dark" v-if="scope.row.auditFlag == 3">不通过</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="初审时间" align="center" prop="auditTime" width="180"/>
      <el-table-column label="终审时间" align="center" prop="endAuditTime" width="180" />
      <el-table-column label="发布标志" align="center" prop="releaseFlag" width="100">
        <template slot-scope="scope">
          <el-tag type="danger" effect="dark" v-if="scope.row.releaseFlag == 0">未发布</el-tag>
          <el-tag effect="dark" v-if="scope.row.releaseFlag == 1">已发布</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="点击量" align="center" prop="browseNum" width="80" v-if="serviceInfo == 2 || serviceInfo == 3" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="240">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.auditFlag == '0' || scope.row.auditFlag == '3'"
            type="text"
            icon="el-icon-edit"
            @click="handleAudit(scope.row, 1)"
            v-hasPermi="['management:content:audit']"
          >
            初审
          </el-button>
          <el-button v-if="scope.row.auditFlag == '1'" type="text" icon="el-icon-edit" @click="handleAudit(scope.row, 2)" v-hasPermi="['management:content:endAudit']">
            终审
          </el-button>
          <el-button
            v-if="scope.row.auditFlag == '2' && scope.row.releaseFlag == '0'"
            type="text"
            icon="el-icon-edit"
            @click="handleAudit(scope.row, 2)"
            v-hasPermi="['management:content:endAudit']"
          >
            取消终审
          </el-button>
          <el-button
            v-if="scope.row.auditFlag == '2' && scope.row.releaseFlag == '0'"
            type="text"
            icon="el-icon-position"
            @click="handleSend(scope.row, 1)"
            v-hasPermi="['management:content:release']"
          >
            发布
          </el-button>
          <el-button
            v-if="scope.row.auditFlag == '2' && scope.row.releaseFlag == '1'"
            type="text"
            icon="el-icon-position"
            @click="handleSend(scope.row, 0)"
            v-hasPermi="['management:content:release']"
          >
            取消发布
          </el-button>
          <el-button
            v-if="scope.row.auditFlag == 0||scope.row.auditFlag == 3"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['management:content:audit', 'management:content:endAudit']"
          >
            修改
          </el-button>
          <el-button v-if="scope.row.auditFlag == '0'" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['management:content:remove']">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改内容管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" :close-on-click-modal="false" width="75%" append-to-body>
      <el-steps  align-center style="margin-bottom: 20px;">
        <el-step title="填写内容"></el-step>
        <el-step title="内容初审"></el-step>
        <el-step title="内容终审"></el-step>
        <el-step title="内容发布"></el-step>
      </el-steps>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="信息类型" prop="serviceInfo">
          <el-select v-model="form.serviceInfo" disabled placeholder="请选择信息类型">
            <el-option v-for="dict in serviceInfoOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标题" prop="title"><el-input v-model="form.title" placeholder="请输入标题" /></el-form-item>
        <el-form-item label="作者" prop="author"><el-input v-model="form.author" placeholder="请输入作者" /></el-form-item>
        <el-form-item label="点击量" prop="browseNum">
          <el-input-number v-model="form.browseNum" :min="1" :step="10" placeholder="请输入点击量" />
        </el-form-item>
        <el-form-item label="封面图片"><imageUpload :limit="1" v-model="form.showPictures" /></el-form-item>
        <el-form-item label="详情" prop="content"><editor v-model="form.content" :min-height="200" /></el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="form.auditFlag == null || form.auditFlag == 0||form.auditFlag == 3">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 审核 -->
    <el-dialog :title="auditTitle" :visible.sync="auditOpen" :close-on-click-modal="false" width="50%" append-to-body>
      <el-steps  align-center style="margin-bottom: 20px;">
        <el-step title="填写内容"></el-step>
        <el-step title="内容初审"></el-step>
        <el-step title="内容终审"></el-step>
        <el-step title="内容发布"></el-step>
      </el-steps>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="信息类型" prop="serviceInfo">
          <el-select v-model="form.serviceInfo" disabled placeholder="请选择信息类型">
            <el-option v-for="dict in serviceInfoOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标题" prop="title"><el-input v-model="form.title" disabled placeholder="请输入标题" /></el-form-item>
        <el-form-item label="作者" prop="author"><el-input v-model="form.author" disabled placeholder="请输入作者" /></el-form-item>
        <el-form-item label="审核状态" prop="auditFlag">
          <el-radio-group v-model="form.auditFlag" disabled>
            <el-radio v-for="dict in auditFlagOptions" :key="dict.dictValue" :label="dict.dictValue">{{ dict.dictLabel }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <template v-if="flag == 1">
          <el-form-item label="初审信息" prop="auditMsg"><el-input v-model="form.auditMsg" placeholder="初审信息" /></el-form-item>
          <el-form-item label="初审时间" prop="auditTime"><el-input v-model="form.auditTime" disabled placeholder="初审时间" /></el-form-item>
        </template>
        <template v-if="flag == 2">
          <el-form-item label="终审信息" prop="endAuditMsg"><el-input v-model="form.endAuditMsg" placeholder="终审信息" /></el-form-item>
          <el-form-item label="终审时间" prop="endAuditTime"><el-input v-model="form.endAuditTime" disabled placeholder="终审时间" /></el-form-item>
        </template>
        <template v-if="flag == 3">
          <el-form-item label="发布状态" prop="releaseFlag">
            <el-radio-group v-model="form.releaseFlag" disabled>
              <el-radio v-for="dict in releaseFlagOptions" :key="dict.dictValue" :label="dict.dictValue">{{ dict.dictLabel }}</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="发布时间" prop="releaseTime"><el-input v-model="form.releaseTime" placeholder="发布时间" /></el-form-item>
        </template>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <template v-if="flag == 1">
          <el-button type="primary" @click="updateAudit(1)">初审通过</el-button>
          <el-button type="danger" @click="updateAudit(3)">初审不通过</el-button>
        </template>
        <template v-if="flag == 2">
          <el-button type="primary" @click="updateAudit(2)">终审通过</el-button>
          <el-button type="danger" @click="updateAudit(3)">终审不通过</el-button>
        </template>
        <template v-if="flag == 3">
          <el-button v-if="form.releaseFlag == 0" type="primary" @click="updateRelease(1)">发布</el-button>
          <el-button v-if="form.releaseFlag == 1" type="warning" @click="updateRelease(0)">取消发布</el-button>
        </template>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listContent, getContent, delContent, addContent, updateContent, exportContent, auditContent, endAuditContent, releaseContent } from '@/api/management/content';

export default {
  name: 'Content',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 内容管理表格数据
      contentList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      //默认信息类型
      serviceInfo: 0,
      // 信息类型字典
      serviceInfoOptions: [],
      // 审核状态字典
      auditFlagOptions: [],
      // 发布标志字典
      releaseFlagOptions: [],
      // 查询参数
      queryParams: {
        title: null,
        pageNum: 1,
        pageSize: 10,
        serviceInfo: null,
        auditFlag: null,
        releaseFlag: null,
        orderByColumn: 'createTime', //排序字段
        isAsc: 'desc' //排序方式
      },
      flag: 0,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [{ required: true, message: '标题不能为空' }, { min: 2, max: 30, message: '用户名称长度必须介于 2 和 30 之间' }],
        author: [{ required: true, message: '作者不能为空' }],
        browseNum: [{ required: true, message: '点击量不能为空' }],
        content: [{ required: true, message: '详情不能为空' }]
      },
      auditTitle: '',
      auditOpen: false,
      baseUrl: process.env.VUE_APP_BASE_API
    };
  },
  created() {
    var query = this.$route.query;
    this.queryParams.serviceInfo = query.serviceInfo;
    this.serviceInfo = query.serviceInfo;
    this.getList();
    this.getDicts('service_info').then(response => {
      this.serviceInfoOptions = response.data;
    });
    this.getDicts('more_audit_flag').then(response => {
      this.auditFlagOptions = response.data;
    });
    this.getDicts('release_flag').then(response => {
      this.releaseFlagOptions = response.data;
    });
  },
  methods: {
    /** 查询内容管理列表 */
    getList() {
      this.loading = true;
      listContent(this.queryParams).then(response => {
        this.contentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 信息类型字典翻译
    serviceInfoFormat(row, column) {
      return this.selectDictLabel(this.serviceInfoOptions, row.serviceInfo);
    },
    // 审核状态字典翻译
    auditFlagFormat(row, column) {
      return this.selectDictLabel(this.auditFlagOptions, row.auditFlag);
    },
    // 发布标志字典翻译
    releaseFlagFormat(row, column) {
      return this.selectDictLabel(this.releaseFlagOptions, row.releaseFlag);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.auditOpen = false;
      this.flag = 0;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        contentId: null,
        serviceInfo: this.serviceInfo,
        title: null,
        author: null,
        showPictures: null,
        briefContent: null,
        content: null,
        createTime: null,
        createAt: null,
        auditFlag: null,
        auditAt: null,
        auditTime: null,
        auditMsg: null,
        releaseFlag: '0',
        releaseTime: null,
        releaseAt: null,
        endAuditAt: null,
        endAuditTime: null,
        endAuditMsg: null
      };
      this.resetForm('form');
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.queryParams.serviceInfo = this.serviceInfo;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm');

      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.contentId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = '添加内容管理';
    },
    /**仅查看内容*/
    handleView(row) {
      this.reset();
      const contentId = row.contentId || this.ids;
      getContent(contentId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = '查看内容';
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const contentId = row.contentId || this.ids;
      getContent(contentId).then(response => {
        this.form = response.data;
        if (this.form.auditFlag == 0||this.form.auditFlag==3) {
          this.open = true;
          this.title = '修改内容管理';
        } else {
          this.$notify.error({ title: '操作驳回', message: '当前内容禁止修改,如需修改请先取消发布和审核' });
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.contentId != null) {
            updateContent(this.form).then(response => {
              this.msgSuccess('修改成功');
              this.open = false;
              this.getList();
            });
          } else {
            addContent(this.form).then(response => {
              this.msgSuccess('新增成功');
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const contentIds = row.contentId || this.ids;
      this.$confirm('是否确认删除内容管理编号为"' + contentIds + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(function() {
          return delContent(contentIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess('删除成功');
        })
        .catch(() => {});
    },
    handleAudit(row, type) {
      this.flag = type;
      this.reset();
      const contentId = row.contentId || this.ids;
      getContent(contentId).then(response => {
        this.form = response.data;
        this.auditOpen = true;
        if (type == 1) {
          this.auditTitle = '内容初审';
        } else {
          this.auditTitle = '内容终审';
        }
      });
    },
    handleSend(row, type) {
      this.flag = 3;
      this.reset();
      const contentId = row.contentId || this.ids;
      getContent(contentId).then(response => {
        this.form = response.data;
        this.auditOpen = true;
        if (type == 1) {
          this.auditTitle = '内容发布';
        } else {
          this.auditTitle = '内容取消发布';
        }
      });
    },
    updateAudit(type) {
      this.form.auditFlag = type;
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.contentId != null) {
            if (type == 1) {
              auditContent(this.form).then(response => {
                this.msgSuccess('初审成功');
                this.auditOpen = false;
                this.getList();
              });
            } else if (type == 2) {
              endAuditContent(this.form).then(response => {
                this.msgSuccess('终审成功');
                this.auditOpen = false;
                this.getList();
              });
            } else {
              auditContent(this.form).then(response => {
                this.msgSuccess('取消成功');
                this.auditOpen = false;
                this.getList();
              });
            }
          }
        }
      });
    },
    updateRelease(type) {
      this.form.releaseFlag = type;
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.contentId != null) {
            if (type == 1) {
              releaseContent(this.form).then(response => {
                this.msgSuccess('发布成功');
                this.auditOpen = false;
                this.getList();
              });
            } else {
              releaseContent(this.form).then(response => {
                this.msgSuccess('取消成功');
                this.auditOpen = false;
                this.getList();
              });
            }
          }
        }
      });
    }
  }
};
</script>
<style lang="scss">
.lsw_square {
  border-radius: 5px;
  width: 70%;
}
</style>
